package cn.tedu.zhyl.base.response;

import lombok.Data;

@Data
public class JsonResult<T> {
    private String msg;
    private Integer code;
    private T data;

    // 私有化构造
    private JsonResult(){}

    // 构建数据
    public static <T> JsonResult build(T data,Integer code,String msg){
        JsonResult<T> jsonResult = new JsonResult();
        jsonResult.setData(data);
        jsonResult.setMsg(msg);
        jsonResult.setCode(code);
        return jsonResult;
    }

    // 对外提供返回方法
    public static <T> JsonResult build(T data,StatusCode statusCode){
        return build(data,statusCode.getCode(),statusCode.getMsg());
    }

    //成功
    public static <T> JsonResult build(T data){
        return build(data,StatusCode.SUCCESS_OPERATION);
    }

    public static <T> JsonResult success(T data){
        return build(data,StatusCode.SUCCESS_OPERATION);
    }

    public static <T> JsonResult error(T data){
        return build(data,StatusCode.FAIL_OPERATION);
    }


    public static <T> JsonResult ok(T data) { return success(data); }
    public static JsonResult ok() { return success(null); }

    public static <T> JsonResult fail(T data) { return error(data); }
    public static JsonResult fail() { return error(null); }
}
